package com.exam.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.entity.College;
import com.exam.demo.entity.Teacher;
import com.exam.demo.service.TeacherService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    TeacherService teacherService;

    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize,
                       @RequestParam(defaultValue = "") String teacherName){
//        return Result.success(teacherService.findPage(new Page<>(pageNum,pageSize),teacherName));
        Page<Teacher> page = teacherService.findPage(new Page<>(pageNum,pageSize),teacherName);
        return Result.success(page);
    }

    //查找全部教师数据
    @GetMapping("/findAll")
    public Result findPage(){
        return Result.success(teacherService.list());
    }

    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody Teacher teacher){
        long count = teacherService.count(new QueryWrapper<Teacher>().eq("teacher_id", teacher.getTeacherId()));
        if(count > 0){
            boolean update = teacherService.update(teacher, new UpdateWrapper<Teacher>().eq("teacher_id", teacher.getTeacherId()));
            if(update){
                return Result.success("修改成功");
            }
        }else{
            boolean save = teacherService.save(teacher);
            if(save){
                return Result.success("添加成功");
            }
        }
        return Result.error("10000","编辑失败");
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(teacherService.removeById(id));
    }


}
